#
# Classifier for Mercury bounce messages
#
package Mail::Bounce::Mercury;

use strict;
use vars qw(@ISA @patterns);

@ISA = qw(Mail::Bounce::Heuristic);

# First Mercury encountered.
# Multipart.
$patterns[0] = << 'EoF';
With reference to your message with the subject:
   ${SUBJECT-.*}

The local mail transport system has reported the following problems
it encountered while trying to deliver your message:

-------------------------------------------------------------------
   User <${FINAL_RECIPIENT-\S+}> ${DIAGNOSTIC-.*}
-------------------------------------------------------------------

Your mail message is being returned to you in the next part of this
message.

Should you need assistance, please mail ${POSTMASTER-\S+}.
EoF

# Mercury 1.43 at cip.hx.uni-paderborn.de
# Single-part
$patterns[1] = << 'EoF';
With reference to your message with the subject:
   ${SUBJECT-.*}

One or more addresses in your message have failed with the following
responses from the mail transport system:

   User <${FINAL_RECIPIENT-\S+}> ${DIAGNOSTIC-.*}.

No more users on this server, should you need assistance, please mail ${POSTMASTER-.*}.



-------------------- Returned message follows ---------------------
EoF

# Mercury 1.13 at reze-1.rz.rwth-aachen.de
$patterns[2] = << 'EoF';
With reference to your message with the subject:
   ${SUBJECT-.*}

One or more addresses in your message have failed with the following
responses from the mail transport system:

   User <${FINAL_RECIPIENT-\S+}> ${DIAGNOSTIC-.*}.

Should you need assistance, please mail ${POSTMASTER-.*}.

-------------------- Returned message follows ---------------------
EoF

sub validate {
    my $self = shift;
    my $body;
    if ($self->is_multipart()) {
	$body = $self->parts(0)->bodyhandle;
    } else {
	$body = $self->bodyhandle;
    }
    return 0 if !$body;
    my $keywords = $self->patternmatch($body->as_string, @patterns);
    return defined($keywords);
}

    

sub recipients {
    my $self = shift;

    if (wantarray()) {
	my $res;
	my $body;
	if ($self->is_multipart()) {
	    $body = $self->parts(0)->bodyhandle;
	} else {
	    $body = $self->bodyhandle;
	}
	die "No body" if !$body;
	my $keywords = $self->patternmatch($body->as_string, @patterns);
	if (defined($keywords)) {
	    $res = new Mail::Bounce::Address;
	    $res->type("permfail");
	    $res->action("failed");
	    my $diag = $$keywords{DIAGNOSTIC};
	    if ($diag eq "not known at this site") {
		$res->status("5.1.1 (Unknown user)");
	    } else {
		$res->status("5.0.0");
	    }
	    $res->diagnostic("Mercury; $diag");
	    $res->final_recipient($$keywords{FINAL_RECIPIENT});
	} else {
	    die "Postfix: Unmatched pattern in recipients()\n";
	}
	return $res;
    } else {
	return 1;
    }
}
1;




